<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PibsController
 *
 * @author Regan
 */
App::import('Vendor', 'importer', array('file'=> 'MDBImport' . DS . 'Exporter.php'));

class PibsController extends AppController {
    public function index()
    {
        $this->loadModel('Header');
//        $allHeader = $this->Header->find('all', array(
//            'fields' => array(
//                'CAR',
//                'PibTg',
//                'isEdited'
//            )
//            ));
        
        if($this->request->data == null)
        {
            $conditions = '';
        }
        else {
            switch($this->request->data['false']['SearchBy'])
            {
                case 'AJU'
                    : $conditions = array('Header.CAR Like ' => '%'. $this->request->data['false']['AJU'] .'%');
                    break;
                default
                    : $conditions = array('Header.PibTg' 
                        => $this->request->data['false']['TglPendaftaran']['year'] . '-'
                            . $this->request->data['false']['TglPendaftaran']['month'] . '-'
                            . $this->request->data['false']['TglPendaftaran']['day']
                        );
                    break;
            }
        }
        
        $allHeader = $this->paginate(
                'Header'
                , $conditions
                );
        
        $this->set('allHeader', $allHeader);
    }
    
    public function view()
    {
        
    }
    
    
    public function cusresexportconfirm($CAR)
    {
        if(isset($this->request->data['generate']))
        {
            $this->redirect(array('action' => 'cusresexport', $this->request->data['Header']['CAR']));
        }
        else if(isset($this->request->data['cancel']))
        {
            $this->redirect(array('action' => 'index'));
        }
        else
        {
            if($CAR == null)
            {
                $this->redirect(array('action' => 'index'));
                $this->Session->setFlash('No AJU Tidak di supply');
            }

            $this->loadModel('Header');
            $objHeader = $this->Header->find('first', array(
                'conditions' => array(
                    'Header.CAR' => $CAR
                )
            ));

            $this->request->data = $objHeader;
        }
    }
    
    public function cusresexport($CAR)
    {
        //$this->view('cusresexportconfirm');
        
        $objExporter = new Exporter();
        $result = $objExporter->CUSRESExport($CAR);
        
        if(file_exists($result['fileResult'])) 
        { 
            $this->response->file($result['fileResult']); 
            $this->response->download($result['fileResult']);
        }
        
        return $this->response;
        //$this->redirect(array('action' => 'index'));
    }
    
    public function editlinkinvoice($CAR)
    {
        $this->loadModel('Invoicedetail');
        $this->loadModel('Iteminvoicemapping');
        $this->loadModel('Header');
        
        $objlist = $this->Invoicedetail->getInvoiceList($CAR);
        
        $this->set('invoicelist', $objlist);
        
        $counter = 1;
        $counterdtl = 1;

        $datainvoice = $this->Invoicedetail->getDataInvoice($CAR);

        foreach($datainvoice as $obj)
        {
            $uivalue['Invoice']['InvoiceNo_' . $counter] = $obj['a']['DokNo'];
            $uivalue['Invoice']['InvoiceLevel_' . $counter] = $obj['0']['InvoiceLevel'];
            $uivalue['Invoice']['ShipRef_' . $counter] = $obj['0']['ShipRef'];

            $invoicelabel[$counter] = $obj['a']['DokNo'];

            $counter++;
        }

        $datadetail = $this->Iteminvoicemapping->getDataDetail($CAR);

        foreach($datadetail as $objdetail)
        {
            $uivalue['Invoice']['Serial_' . $counterdtl] = $objdetail['a']['Serial'];
            $uivalue['Invoice']['BrgUrai_' . $counterdtl] = $objdetail['a']['BrgUrai'];
            $uivalue['Invoice']['JmlSat_' . $counterdtl] = $objdetail['a']['JmlSat'];
            $uivalue['Invoice']['InvoiceNoLink_' . $counterdtl] = $objdetail['0']['DokNo'];
            $uivalue['Invoice']['ProductLine_' . $counterdtl] = $objdetail['0']['ProductLine'];
            $uivalue['Invoice']['ExternalID_' . $counterdtl] = $objdetail['0']['ExternalID'];

            $detaillabel[$counterdtl]['Serial'] = $objdetail['a']['Serial'];
            $detaillabel[$counterdtl]['BrgUrai'] = $objdetail['a']['BrgUrai'];
            $detaillabel[$counterdtl]['JmlSat'] = $objdetail['a']['JmlSat'];

            $counterdtl++;
        }
        
        $this->set('counter', $counter);
        $this->set('counterdtl', $counterdtl);
        $this->set('invoicelabel', $invoicelabel);
        $this->set('detaillabel', $detaillabel);
        
        if($this->request->is('post'))
        {
            //$this->set('check2',$this->request->data);
            $counter = $this->request->data['Invoice']['counter'];
            $counterdtl = $this->request->data['Invoice']['counterdtl'];
            
            $invoicesave['Invoicedetail']['createdBy'] = $this->Auth->user('username');
            $detailsave['Iteminvoicemapping']['createdBy'] = $this->Auth->user('username');
            
            for($i=1 ; $i < $counter ; $i++)
            {
                $invoicesave['Invoicedetail']['DokNo'] = $this->request->data['Invoice']['InvoiceNo_' . $i];
                $invoicesave['Invoicedetail']['InvoiceLevel'] = $this->request->data['Invoice']['InvoiceLevel_' . $i];
                $invoicesave['Invoicedetail']['ShipRef'] = $this->request->data['Invoice']['ShipRef_' . $i];
                
                $this->Invoicedetail->RecordSave($invoicesave);
            }
            
            $detailsave['Iteminvoicemapping']['CAR'] = $CAR;
            
            for($i=1 ; $i < $counterdtl ; $i++)
            {
                $detailsave['Iteminvoicemapping']['Serial'] = $this->request->data['Invoice']['Serial_' . $i];
                $detailsave['Iteminvoicemapping']['DokNo'] = $this->request->data['Invoice']['InvoiceNoLink_' . $i];
                $detailsave['Iteminvoicemapping']['ProductLine'] = $this->request->data['Invoice']['ProductLine_' . $i];
                $detailsave['Iteminvoicemapping']['ExternalProductId'] = $this->request->data['Invoice']['ExternalID_' . $i];
                
                $this->Iteminvoicemapping->RecordSave($detailsave);
            }
            
            $this->Header->EditHeader($CAR);
        }
        else
        {
            $this->request->data = $uivalue;
        }
    }
}
